﻿from http.server import BaseHTTPRequestHandler, HTTPServer

# ------------------------------
# 用户数据类（等价于仓颉语言 User）
# ------------------------------
class User:
    def __init__(self, id: int, name: str, email: str, about: str):
        self.id = id
        self.name = name
        self.email = email
        self.about = about

    def to_json(self):
        return f'{{"id":{self.id},"name":"{self.name}","email":"{self.email}","about":"{self.about}"}}'


# 全局用户存储
users = [
    User(251001, "张三", "zhangsan@example.com", "杭州"),
    User(251002, "李四", "lisi@example.com", "宁波"),
]
nextUserId = 3  # 对应 nextUserId = 3


# ------------------------------
# HTTP 请求处理器
# ------------------------------
class RequestHandler(BaseHTTPRequestHandler):

    def do_GET(self):
        if self.path == "/":  # 主页面
            self.render_user_list()
        else:
            self.send_404()

    # --------------------------
    # 生成用户表格的页面
    # --------------------------
    def render_user_list(self):
        # ① HTML 头部
        html = """
        <!DOCTYPE html>
        <html lang="zh-CN">
        <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1">
            <title>用户管理系统</title>
            <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
            <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.5/font/bootstrap-icons.css">
            <style>
                body{background:linear-gradient(135deg, #667eea 0%, #764ba2 100%);min-height:100vh;padding:40px 0}
                .card-shadow{border:none;border-radius:1rem;box-shadow:0 8px 25px rgba(0,0,0,.15)}
                .table-hover tbody tr:hover{background-color:#f1f5ff}
            </style>
        </head>
        <body>
            <div class="container">
                <div class="row justify-content-center">
                    <div class="col-12 col-lg-10">
                        <div class="card card-shadow p-4">
                            <div class="d-flex justify-content-between align-items-center mb-3">
                                <h2 class="mb-0"><i class="bi bi-people-fill text-primary"></i> 已注册用户</h2>
                                <a class="btn btn-primary" href="/add"><i class="bi bi-person-plus"></i> 新增用户</a>
                            </div>
                            <div class="table-responsive">
                                <table class="table table-hover align-middle text-center">
                                    <thead class="table-light">
                                        <tr><th>ID</th><th>姓名</th><th>邮箱</th><th>备注</th></tr>
                                    </thead>
                                    <tbody>
        """

        # ② 插入用户行
        for user in users:
            html += f"""
                                    <tr>
                                        <td>{user.id}</td>
                                        <td><strong>{user.name}</strong></td>
                                        <td><a href="mailto:{user.email}" class="text-decoration-none">{user.email}</a></td>
                                        <td><span class="badge bg-secondary">{user.about}</span></td>
                                    </tr>
            """

        # ③ 尾部
        count = len(users)
        html += f"""
                                    </tbody>
                                </table>
                            </div>
                            <div class="text-muted small mt-3">
                                共 <span class="fw-bold">{count}</span> 位用户
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
        </body>
        </html>
        """

        # 输出响应
        self.send_response(200)
        self.send_header("Content-Type", "text/html; charset=UTF-8")
        self.end_headers()
        self.wfile.write(html.encode("utf-8"))

    # 404 页面
    def send_404(self):
        self.send_response(404)
        self.send_header("Content-Type", "text/plain; charset=UTF-8")
        self.end_headers()
        self.wfile.write("404 Not Found".encode("utf-8"))


# ------------------------------
# 启动服务器
# ------------------------------
def main():
    host = "127.0.0.1"
    port = 8080

    server = HTTPServer((host, port), RequestHandler)
    
    print("系统已启动!")
    print("请在浏览器中访问: http://127.0.0.1:8080/")
    print("按 Ctrl+C 停止服务...")

    server.serve_forever()


if __name__ == "__main__":
    main()
